package com.wing.sell.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wing.common.exception.BusinessException;
import com.wing.purchase.model.entity.SupplierCategory;
import com.wing.sell.dao.AgentCategoryDao;
import com.wing.sell.model.entity.AgentCategory;
import com.wing.sell.model.request.AgentCategoryForm;
import com.wing.sell.model.response.AgentCategoryVO;
import com.wing.sell.service.AgentCategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 代理商类别-ServiceImpl
 *
 * @author
 */
@Service
@Slf4j
public class AgentCategoryServiceImpl extends ServiceImpl<AgentCategoryDao, AgentCategory> implements AgentCategoryService {
    private long ROOT_ID=-1l;
    @Override
    public int add(AgentCategoryForm agentCategoryForm) {

        //校验

        AgentCategory agentCategory =new AgentCategory();
        CopyOptions copyOptions = CopyOptions.create()
        .setEditable(AgentCategory.class)
        .setIgnoreError(true)
        .setIgnoreNullValue(true);
        BeanUtil.copyProperties(agentCategoryForm, agentCategory, copyOptions);

        return baseMapper.insert(agentCategory);
    }

    @Override
    public List<AgentCategoryVO> list4app(Map<String, Object> params) {
        return baseMapper.list4app(params);
    }

    @Override
    public Page<AgentCategoryVO> pageList4app(Page page, Map<String, Object> params) {
        List<AgentCategoryVO> list = baseMapper.list4app(page, params);
        page.setRecords(list);
        return page;
    }

    @Override
    public AgentCategoryVO get4appById(Long id) {
        return baseMapper.get4appById(id);
    }


    @Override
    public int enable(Long id) {
        AgentCategoryVO agentCategoryVO=baseMapper.get4appById(id);
        if (agentCategoryVO!=null){
            AgentCategory agentCategory =new AgentCategory();
            agentCategory.setId(id);
            agentCategory.setIsDeleted(true);
            int updateCount=baseMapper.updateById(agentCategory);
            if (agentCategoryVO.getParentId()!=ROOT_ID){
                AgentCategory agentCategory1=new AgentCategory();
                agentCategory1.setId(agentCategoryVO.getParentId());
                agentCategory1.setHasEnableChild(true);
                baseMapper.updateById(agentCategory1);
            }
            return updateCount;
        }
        return 0;
    }

    @Override
    public int disable(Long id) {
        AgentCategoryVO agentCategoryVO=baseMapper.get4appById(id);
        if (agentCategoryVO!=null){
            AgentCategory agentCategory =new AgentCategory();
            agentCategory.setId(id);
            agentCategory.setIsDeleted(false);
            int updateCount=baseMapper.updateById(agentCategory);
            if (agentCategoryVO.getParentId()!=ROOT_ID){
                int count=baseMapper.selectCount(new QueryWrapper<AgentCategory>().eq("parent_id",agentCategoryVO.getParentId()).eq("is_deleted",true));
                if (count==0){
                    AgentCategory agentCategory1=new AgentCategory();
                    agentCategory1.setId(agentCategoryVO.getParentId());
                    agentCategory1.setHasEnableChild(false);
                    baseMapper.updateById(agentCategory1);
                }
            }

            return updateCount;
        }
        return 0;
    }

    @Override
    public boolean delete(Long agentCategoryId) {
        // 查询当前删除分类是否存在子类
        int count = baseMapper.selectCount(new QueryWrapper<AgentCategory>().eq("parent_id", agentCategoryId));
        if(count > 0){
            throw new BusinessException(500, "请先删除子分类");
        }
        return removeById(agentCategoryId);
    }
}
